﻿Public Class ViewProduct

    Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click
        Dim frm = New Product
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            loaddata()
        End If
    End Sub

    Private Sub btnupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnupdate.Click
        If Me.DataGridView1.SelectedRows.Count = 0 Then
            Exit Sub
        End If

        Dim row = Me.DataGridView1.SelectedRows(0)
        Dim frm As New Product
        frm.l.Text = row.Cells("ProductId").Value
        frm.txtcode.Text = row.Cells("ProductCode").Value
        frm.txtname.Text = row.Cells("ProductName").Value
        frm.txtprice.Text = row.Cells("Price").Value
        frm.cate = row.Cells("CategoryId").Value
        frm.txtminprice.Text = row.Cells("MinimumPrice").Value
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            loaddata()
        End If
    End Sub

    Private Sub loaddata()
        cmd.Parameters("@S") = txtSearch.Text
        DataGridView1.DataSource = cmd.ExecuteDataTable("SELECT ProductCode,ProductName,ProductId,p.CategoryId,Price,p.MinimumPrice,c.CategoryName FROM Product p INNER JOIN Category c on p.CategoryId=c.CategoryId WHERE p.Active=1 AND UPPER(ProductName) LIKE UPPER('%'+@S+'%');")
    End Sub
    Private Sub ViewProduct_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        UI.FormatDataGrid(DataGridView1)

    End Sub

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()
        loaddata()
        ' Add any initialization after the InitializeComponent() call.

    End Sub

    Private Sub btndelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndelete.Click
        If Me.DataGridView1.SelectedRows.Count = 0 Then
            Exit Sub
        End If

        If MessageBox.Show("Are you sure want to delete ?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) <> Windows.Forms.DialogResult.Yes Then
            Exit Sub
        End If
        Dim row = Me.DataGridView1.SelectedRows(0)
        Dim id = row.Cells("ProductId").Value

        cmd.Parameters("@Id") = id
        cmd.ExecuteNonQuery("UPDATE Product SET Active=0 WHERE ProductId=@Id")
        loaddata()
    End Sub

    Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
        loaddata()
    End Sub
End Class